package net.lainiao.leetcode;

import net.lainiao.leetcode.base.Common;
import net.lainiao.leetcode.base.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class LintCode66 {
    public static void main(String[] args) {
//        二叉树前序遍历
        TreeNode root=new TreeNode(2);
        root.left = new   TreeNode(1);
        root.right = new  TreeNode(3);
        LintCode66 code=new LintCode66();
        List<Integer> result=code.preorderTraversal(root);
        Common.showJsonForObject(result,false);
    }

    public List<Integer> preorderTraversal(TreeNode root) {
        if(root==null){
            return null;
        }

        List<Integer> list=new ArrayList<>();
        list.add(root.val);
        List<Integer> left=preorderTraversal(root.left);
        List<Integer> right=preorderTraversal(root.right);
        if(left!=null){
            list.addAll(left);
        }
        if(right!=null){
            list.addAll(right);
        }
        return list;
    }



}
